import type { TableColumnCtx } from 'element-plus';
import type { DefaultRow } from 'element-plus/es/components/table/src/table/defaults';

import type { VNode } from 'vue';

export interface TableColumnContext<T extends DefaultRow = any> extends Partial<Omit<TableColumnCtx<T>, 'renderCell' | 'renderHeader'>> {
    visible?: boolean;
    tag?: boolean;
    tooltip?: boolean;
    collapseButtons?: boolean;
    maxCollapseButtons?: number;
    options?: Array<{ [key: string]: any }>;
    render?: ((scope: RenderScope) => VNode) | TableColumnActions[]; // 自定义单元格内容渲染（tsx语法）
}

export interface TableSearchFormItem {
    label?: string;
    prop?: string;
    span?: number;
    order?: number;
    value?: any;
}

type ButtonType = '' | 'danger' | 'default' | 'info' | 'primary' | 'success' | 'warning';

export interface TableColumnActions {
    label?: string;
    auth?: string;
    type?: ButtonType;
    icon?: string;
    onClick: (scope: RenderScope) => void;
}

export type RenderScope<T extends DefaultRow = any> = {
    $index: number;
    [key: string]: any;
    column: TableColumnContext<T>;
    row: T;
};

export interface FieldProps {
    label?: string;
    prop?: string;
    search?: boolean | TableSearchFormItem;
    column?: TableColumnContext;
}

export interface DataTableProps {
    api?: string;
    height?: number | string;
    index?: boolean;
    border?: boolean;
    stripe?: boolean;
    pageSize?: number;
    params?: Record<string, any>;
    primaryKey?: string;
    columns?: Array<FieldProps>;
    lazy?: boolean;
    pagination?: boolean;
    selection?: boolean;
    selectOnRowClick?: boolean;
    contextMenuExtra?: Array<any> | { [key: number]: Array<any> };
    formItemWidth?: number;
    beforeOpenContextMenu?: (data: { current: any; selections: Array<any> }, menus: Array<any>) => boolean;
}

export interface DynamicsColumnFieldProps {
    prop: string;
    label: string;
    visible: boolean;
    no: number;
    render?: ((scope: RenderScope) => VNode) | TableColumnActions[];
    attrs: TableColumnContext;
}

export type OnActionClickParams<T = RenderScope> = {
    code: string;
    scope: T;
};
export type OnActionClickFn<T = RenderScope> = (params: OnActionClickParams<T>) => void;
